//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design;
using Microsoft.Research.ScientificWorkflow.NetCDF.CSharpAPI;
using System.Workflow.ComponentModel.Compiler;

using Microsoft.Research.ScientificWorkflow;
using Microsoft.Research.ScientificWorkflow.Helpers;

using WorkflowData;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Hyper Cube Difference Transform")]
    [WorkflowCategory("NetCdf")]
    [Description("Does the difference of the data for the Two HC objects")]
    public class DifferenceTransformActivity : Activity
	{
		public DifferenceTransformActivity()
		{
			InitializeComponent();
		}

		#region Designer generated code

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		[System.Diagnostics.DebuggerNonUserCode]
		private void InitializeComponent()
		{
			this.Name = "DifferenceTransformActivity";
		}

		#endregion

		#region Dependency Properties

        public static DependencyProperty FirstCubeProperty =
            DependencyProperty.Register("FirstCube", typeof(HyperCube), typeof(DifferenceTransformActivity));

        [RequiredInputParam]
        [Name("First Hyper Cube")]
        [Description("First Hyper Cube for Merging")]
        public HyperCube FirstCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(DifferenceTransformActivity.FirstCubeProperty)));
            }
            set
            {
                base.SetValue(DifferenceTransformActivity.FirstCubeProperty, value);
            }
        }

        public static DependencyProperty SecondCubeProperty =
            DependencyProperty.Register("SecondCube", typeof(HyperCube), typeof(DifferenceTransformActivity));

        [RequiredInputParam]
        [Name("Second Hyper Cube")]
        [Description("Second Hyper Cube for Merging")]
        public HyperCube SecondCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(DifferenceTransformActivity.SecondCubeProperty)));
            }
            set
            {
                base.SetValue(DifferenceTransformActivity.SecondCubeProperty, value);
            }
        }

        public static DependencyProperty DifferenceCubeProperty =
            DependencyProperty.Register("DifferenceCube", typeof(HyperCube), typeof(DifferenceTransformActivity));

        [OutputParam]
        [Name("Resultant Hyper Cube")]
        [Description("Difference Hyper Cube containing diff data for specififed variable from both cubes.")]
        public HyperCube DifferenceCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(DifferenceTransformActivity.DifferenceCubeProperty)));
            }
            set
            {
                base.SetValue(DifferenceTransformActivity.DifferenceCubeProperty, value);
            }
        }

		public static DependencyProperty VariableNamesProperty = DependencyProperty.Register("VariableNames", 
            typeof(string), typeof(DifferenceTransformActivity));

        [RequiredInputParam]
        [Name("Variable Names", "Difference Variables")]
        [Description("Names of the variables whose differences need to be computed")]
		public string VariableNames
		{
			get
			{
				return ((string)(base.GetValue(DifferenceTransformActivity.VariableNamesProperty)));
			}
			set
			{
				base.SetValue(DifferenceTransformActivity.VariableNamesProperty, value);
			}
		}

		public static DependencyProperty OperationProperty = DependencyProperty.Register("Operation", typeof(string), 
            typeof(DifferenceTransformActivity));

        [RequiredInputParam]
        [Name("Operation Type")]
        [Description("The type of arithmetic operation to be done on the HyperCubes")]
		public string Operation
		{
			get
			{
				return ((string)(base.GetValue(DifferenceTransformActivity.OperationProperty)));
			}
			set
			{
				base.SetValue(DifferenceTransformActivity.OperationProperty, value);
			}
		}

		#endregion

		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
            string[] variableNames = (this.VariableNames).Split(new char[] { ',' });
            if (variableNames.Length == 0)
                throw new ArgumentException("Error Converting from String to Variable Array");

			IHyperCubeTransform transformer = new AlgebraOperationTransform();
            this.DifferenceCube = transformer.Transform(this.FirstCube, this.SecondCube, variableNames, 
                (AlgebraOperations)Enum.Parse(typeof(AlgebraOperations), this.Operation));

			return ActivityExecutionStatus.Closed;
		}

	}

	public enum AlgebraOperations
	{
		Addition,
		Difference
	}
}
